<?php
require_once 'rpo/util/AbstractMapReduceStep.php';
require_once 'rpo/http/curl/CURLConnection.php';
require_once 'dso/buscape/AbstractBuscapeAPI.php';

class BuscapeAPIImp extends AbstractBuscapeAPI {
	public function getApplicationId(){
		return '6a46486e764a51354753343d';
	}
}

class MapReduceExample extends AbstractMapReduceStep {
	public function map( MapReduce $mapReduce , $key , $value ){
		if ( $value->isTop5Category() ){
			$mapReduce->map( 'Categoria Top 5' , $value );
		}

		if ( $value->isFinal() ){
			$mapReduce->map( 'Categoria Final' , $value );
		}

		if ( $value->hasOffer() ){
			$mapReduce->map( 'Categoria com Ofertas' , $value );
		}
	}
}

$exemplo = new BuscapeAPIImp( new CURLConnection() );
$mapReduce = new MapReduceExample();

foreach ( $exemplo->findCategoryByID( 2 )->getCategories( true )->mapReduce( $mapReduce , $mapReduce ) as $key => $categories ){
	printf( "%s\n" , $key );

	foreach ( $categories as $category ){
		printf( "\t%5d - %s%s\n" , $category->getId() , $category->getName() , $category->isTop5Category() ? ' *' : null );
	}
}